/**
 * Created on May 23, 2007
 */
package jvs.bsh;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringReader;

import jvs.vfs.FileSystem;
import jvs.vfs.util.Log;
import bsh.Interpreter;

/**
 * @author qiangli
 * 
 */
public class BeanShell {

	/**
	 * Usage: jvs.bsh.BeanShell -d work_dir -f scriptfile -s script
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			//
			java.io.File f = new java.io.File("c:/temp");
			String uri = f.toURI().toString();
			java.lang.System.setProperty("jvs.rootfs", uri);
			java.lang.System.setProperty("jvs.debug", "debug");

			FileSystem fs = FileSystem.getFileSystem();

			fs.boot(uri);
			//
			Log.log(Log.DEBUG,BeanShell.class,java.util.Arrays.asList(args));
			
			
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			final PrintStream out = new PrintStream(bo);
			final StringReader in = new StringReader("");
			Interpreter interp = new Interpreter(in,out,out,false);
			/*
			interp.setConsole(new ConsoleInterface() {
				
				public void error(Object o) {
					System.err.println("error:"+o);
				}

				public PrintStream getErr() {
					// TODO Auto-generated method stub
					return null;
				}

				public Reader getIn() {
					return null;
				}

				public PrintStream getOut() {
					// TODO Auto-generated method stub
					return null;
				}

				public void print(Object o) {
					System.out.println("print:"+o);
				}

				public void println(Object o) {
					System.out.println("println:"+o);
				}
				
			});
			*/
			String file = null;
			String script = null;
			for(int i=0; i<args.length;) {
				String arg = args[i];
				if (arg.equals("-f") || arg.equals("-file") ) {
					file = args[i+1];
					i++;
				} else if (arg.equals("-s") || arg.equals("-script")) {
					script = args[i+1];
					i++;
				}
			}
			if (file != null) {
				interp.source(file);
			}
			
			interp.source("/test.bsh");
			if (script != null) {
				//interp.eval(script);
			} else {
				
				//interp.eval("desktop();");
				//interp.eval("pwd();");
				//interp.eval("cat(\"/build.xml\");");
				//interp.eval("ant(\"/build.xml\",\"hello\");");
			}
			System.out.println(bo.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
